﻿using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseWebsiteBLL
{
    public class ProductCategorieService
    {
        public readonly SqlSugarClient _db;
        public readonly ProductService _productService;

        public ProductCategorieService(SqlSugarHelper sqlSugarHelper, ProductService productService)
        {
            _db = sqlSugarHelper.sugarClient;
            _productService = productService;
        }

        public async Task InsertOrUpdate(IEnumerable<ProductCategorieEntity> productCategories)
        {
            _db.BeginTran();
            foreach (var item in productCategories)
            {
                try
                {
                    item.UpdateDate = DateTime.Now;
                    if (item.Id == 0)
                    {
                        if (string.IsNullOrEmpty(item.CategoryName))
                        {
                            continue;//如果ID == 0 并且 分类名称 == null 直接跳过
                        }
                        item.CreateDate = DateTime.Now;
                        await _db.Insertable(item)
                            .ExecuteCommandAsync();
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(item.CategoryName))
                        {
                            throw new Exception("分类名称不能为空");
                        }
                        await _db.Updateable(item)
                            .Where(p => p.Id == item.Id)
                            .ExecuteCommandAsync();
                    }
                }
                catch (Exception ex)
                {
                    _db.RollbackTran();
                    throw ex;
                }
            }
            _db.CommitTran();
        }

        public async Task<List<ProductCategorieEntity>> GetAllProductCategorie()
        {
            return await _db.Queryable<ProductCategorieEntity>()
                .ToListAsync();
        }

        public async Task<List<ProductCategorieEntity>> GetAllProductCategorie(bool enable = true)
        {
            return await _db.Queryable<ProductCategorieEntity>()
                .Where(p=> p.Enable == enable)
                .ToListAsync();
        }

        public async Task<ProductCategorieEntity> GetProductCategorieById(int id)
        {
            return await _db.Queryable<ProductCategorieEntity>()
                .Where(p => p.Id == id)
                .FirstAsync();
        }

        public async Task DeleteProductCategorieById(int id)
        {
            var entity = await GetProductCategorieById(id);
            if (entity == null)
            {
                throw new Exception($"删除失败，Id {id} 分类不存在");
            }
            else
            {
                var productList = await _productService.GetProductByCategorieId(entity.Id);
                if (productList.Count > 0)
                {
                    throw new Exception($"删除失败，“{entity.CategoryName}”分类存在{productList.Count}条商品，请删除商品后再删除分类");
                }
            }
            await _db.Deleteable<ProductCategorieEntity>()
                .Where(p => p.Id == id)
                .ExecuteCommandAsync();
        }
    }
}
